# -*- coding: utf-8 -*-
# manage.py 顶部（创建 app 之前）
from dotenv import load_dotenv

load_dotenv()  # 会把 .env 读进来

# 顶部新增 ↓↓↓
from flask_migrate import Migrate
from app1 import db


from flask_script import Manager

# werkzeug
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple

# 也可以选择使用 socketio 实现 http 服务和 websocket 服务
# flask_socketio
from flask_socketio import SocketIO, send, emit

from app1 import app
from app2 import appws

# ========= for flask_script ==========================
manager1 = Manager(app)
manager2 = Manager(appws)

# 新增用于绑定migrate
migrate = Migrate(app, db)   # 绑定 migrate

# ========= for werkzeug =====================================
# 使用 DispatcherMiddleware 进行路由分发
dm = DispatcherMiddleware(app, {
    '/ws': appws,  # 根据访问的 URL 前缀进行路由分发
})

from app1.wsserver import socketio1

# 在这里初始化 Flask-SocketIO
# socketio1.init_app(app)

# for socketio
# socketio = SocketIO(app)


if __name__ == '__main__':
    # run python manage.py runserver --host=0.0.0.0 --port=5002
    # manager1.run()
    # manager2.run()

    # run_simple
    # python manage.py
    # run_simple('0.0.0.0', 5002, dm, use_debugger=True, use_reloader=True)

    # python manage.py
    # 启动 Flask 应用，同时提供 HTTP 和 WebSocket 服务
    socketio1.run(app, debug=True, host="0.0.0.0", port=5002)
